package org.robot.scm.pojos.platform;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.enums.ChannelEnum;
import org.robot.scm.pojos.enums.HttpMethodEnum;

@Data
@Builder
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Clean3BaseNotify extends BaseNotify {

    /**
     * 提醒内容
     */
    private String message;

    /**
     * 创建日 2024-03-15
     */
    private String createDay;

    @SneakyThrows
    @Override
    public TargetNotify robotToNotifyBean(String robotId, BaseConfig config) {
        String content = config.robotText(robotId, URLEncoder.encode(message, StandardCharsets.UTF_8.name()));
        return TargetNotify.builder()
                .httpMethodEnum(HttpMethodEnum.POST)
                .channelEnum(ChannelEnum.ROBOT)
                .notifyUrl(config.robotUrl())
                .topic(config.get().getNotifyChannel().getRobot().getBodyText())
                .content(content)
                .build();
    }

    @Override
    public boolean equal(BaseNotify another) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 当前日期在这个月中的天数
        int dayOfMonth = currentDate.getDayOfMonth();
        // 1 ～ 3号之间, 每天提醒一次即可
        boolean isInRange = (dayOfMonth >= 1 && dayOfMonth <= 3);
        // 不在1～3号之间，则返回true，表示两者相同 从而不提醒
        if (!isInRange) {
            return true;
        }

        // 在1～3号之间，且当前时间小于9点不提醒，每小时执行一次的话，应该是8:00~9:00之间才会提醒
        LocalDateTime currentTime = LocalDateTime.now();
        LocalTime eightOclock = LocalTime.of(8, 0, 0);
        if (currentTime.toLocalTime().isBefore(eightOclock)) {
            return true;
        }

        return StringUtils.equals(((Clean3BaseNotify) another).getCreateDay(), createDay);
    }

}
